<?php

namespace Drawping\Amap\Yuntu;

use Drawping\Amap\AbstractAPI;
use GuzzleHttp\Psr7\Request;

class Data extends AbstractAPI
{
    const API_CREATE = 'http://yuntuapi.amap.com/datamanage/data/create';
    const API_UPDATE = 'http://yuntuapi.amap.com/datamanage/data/update';
    const API_DELETE = 'http://yuntuapi.amap.com/datamanage/data/delete';

    function __construct($config)
    {
        parent::__construct($config);
    }

    /**
     * 创建单条数据
     * @param $name
     * @param $location
     * @param string $coordtype
     * @param string $address
     * @return bool array
     */
    function create($name, $location, $coordtype = 'autonavi', $address = '', $custom = []){

        $data = [
            '_name' =>$name,
            '_location' => $location,
            'coordtype' =>$coordtype,
            '_address' =>$address,
        ];
        if($custom){
            $data = array_merge($data, $custom);
        }

        $form_params = [
            'key' => $this->config['app_key'],
            'tableid' => $this->tableid,
            'loctype' => 1,
            'data' => json_encode($data),
        ];


        $response = $this->http->request(
            'POST',
            self::API_CREATE,
            [
                'form_params' => $this->signature($form_params)
            ]
        );
        $r = $this->parseJSON($response);

        return $r ? $r->_id : false;
    }

    /**
     * 更新单条数据
     * @param string $id
     * @param array $custom
     * @param string $name
     * @param string $location
     * @param string $coordtype
     * @param string $address
     * @return bool
     */
    function update($id = '', $custom = [], $name = '', $location = '', $coordtype = 'autonavi', $address = ''){

        $data = [
            '_id' =>$id,
            '_name' =>$name,
            '_location' => $location,
            'coordtype' =>$coordtype,
            '_address' =>$address,
        ];
        if($custom){
            $data = array_merge($data, $custom);
        }

        $form_params = [
            'key' => $this->config['app_key'],
            'tableid' => $this->tableid,
            'loctype' => 1,
            'data' => json_encode($data),
        ];


        $response = $this->http->request(
            'POST',
            self::API_UPDATE,
            [
                'form_params' => $this->signature($form_params)
            ]
        );
        $r = $this->parseJSON($response);

        return $r ? true : false;
    }

    /**
     * 删除数据
     * @param $id
     * @return bool
     */
    function delete($id){
        $form_params = [
            'key' => $this->config['app_key'],
            'tableid' => $this->tableid,
            'ids' => $id,
        ];
        $response = $this->http->request(
            'POST',
            self::API_DELETE,
            [
                'form_params' => $this->signature($form_params)
            ]
        );
        $r = $this->parseJSON($response);

        return $r ? true : false;
    }

}
